package com.evan.kdl.test.day02;

import cn.hutool.core.date.StopWatch;
import com.evan.kdl.test.entity.MyTask;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * cpu有8核 处理器16
 */
public class ParallelDemo {
    public static void main(String[] args) {
        // 方案二：使用并行流
        // step 1: 创建10个MyTask对象，每个任务持续1s，存入List集合
        IntStream intStream = IntStream.range(0, 18);
        List<MyTask> tasks = intStream.mapToObj(item -> new MyTask(1)).collect(Collectors.toList());

        // step 2: 执行10个MyTask,统计总耗时
        StopWatch sw = new StopWatch();
        sw.start();
        List<Integer> results = tasks.parallelStream().map(MyTask::doWork).collect(Collectors.toList());
        long end = System.currentTimeMillis();
        sw.stop();
        System.out.printf("processed %d tasks %.2f second", tasks.size(), sw.getTotalTimeSeconds());
    }
}